﻿using LitJson;
using LNet;
using OestsCommon.Model;
using OestsDataBase;
using OestsServer.Net;
using System.Collections.Generic;

namespace OestsServer.Handlers.OESPHandlers
{
    public class ExamTableHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.ExamTable;

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);

            List<ExamModel> list = ExamDAL.GetData();
            ResponsePacket.DataObject = list;
            client.Send(ResponsePacket);
        }
    }

    public class AddExamHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.AddExam;
        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            ExamModel data = JsonMapper.ToObject<ExamModel>(resquestPacket.DataStr);

            data.TeacherId = client.User.DBid;
            if (ExamDAL.CheckExist(data.Name))
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "已存在考试！";
                client.Send(ResponsePacket);
                return;
            }
            data.Id = ExamDAL.AddExam(data);
            ResponsePacket.ReturnCode = ReturnCode.Success;
            ResponsePacket.DataObject = data;
            client.Send(ResponsePacket);

            //更新试卷的使用次数
            int newCount = PaperDAL.UpdatePaperUseCount(data.PaperId);
            Response response = new Response(PacketKey.UpdatePaperUseCount, new Int2 { Int0 = data.PaperId, Int1 = newCount })
            {
                ReturnCode = ReturnCode.Success
            };

            OESPClientChannel.BrocastToTeacher.Dispatch(response);

        }
    }

    public class DeleteExamHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.DeleteExam;

        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);


            int examid = int.Parse(resquestPacket.DataStr);
            ExamDAL.Delete(examid);
            ResponsePacket.DataObject = examid;
            client.Send(ResponsePacket);
        }
    }


    public class ModifyExamHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.ModifyExam;

        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            ExamModel exam = JsonMapper.ToObject<ExamModel>(resquestPacket.DataStr);
            //以前的试卷
            int oldPaper = ExamDAL.GetExamByID(exam.Id).PaperId;

            // DP<ExamModel, List<int>> data = JsonMapper.ToObject<DP<ExamModel, List<int>>>(resquestPacket.DataStr);

            ExamDAL.Modify(exam);
            //StudentExamDAL.DeleteExamInfo(data.Value1.Id);
            //data.Value2.ForEach(id =>
            //{
            //    StudentExamDAL.AddStudentToExam(data.Value1.Id, id);
            //}
            //);
            ResponsePacket.DataObject = exam;
            client.Send(ResponsePacket);

            //判断是否更改了试卷
            if (oldPaper != exam.PaperId)
            {
                //更新试卷的使用次数

                int newCount = PaperDAL.UpdatePaperUseCount(exam.PaperId);
                Response response = new Response(PacketKey.UpdatePaperUseCount, new Int2 { Int0 = exam.PaperId, Int1 = newCount })
                {
                    ReturnCode = ReturnCode.Success
                };

                OESPClientChannel.BrocastToTeacher.Dispatch(response);
            }

        }
    }

    public class GetTheoryExamListHandler : HandlerBase<OESPClientChannel>
    {
        /// <summary>
        ///平台端获取当前项目的理论考试列表
        /// </summary>
        /// <param name="client"></param>
        /// <param name="packet"></param>
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.GetTheoryExamList;
            }
        }

        public override void Execute(OESPClientChannel client, Request packet)
        {
            base.Execute(client, packet);
            int projectId = int.Parse(packet.DataStr);

            List<ExamModel> data = ExamDAL.GetTheoryExamsByStudent(client.User.DBid, projectId);

            if (data == null || data.Count == 0)
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "当前无理论考试，请联系管理员！";
            }
            else
                ResponsePacket.ReturnCode = ReturnCode.Success;
            ResponsePacket.DataObject = data;
            client.Send(ResponsePacket);

        }
    }
}